package me.zhengjie.modules.weixin.callback;

import me.zhengjie.utils.enums.OrderPrefixEnum;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 王宇
 */
@Component
public class PayCallbackFactory implements ApplicationContextAware {

    private static final Map<OrderPrefixEnum, IPayCallbackService> CALLBACK_MAP = new HashMap<>();

    public static IPayCallbackService prefix(OrderPrefixEnum prefixEnum) {
        return CALLBACK_MAP.get(prefixEnum);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, IPayCallbackService> beans = applicationContext.getBeansOfType(IPayCallbackService.class);
        beans.values().forEach(bean -> CALLBACK_MAP.put(bean.prefix(), bean));
    }
}
